<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script   language="javascript">
<!--

var tables=new Array();

<{foreach from=$tables item=tn name=ind}>
var <{$tn->tableName}>_columns=new Array();

<{foreach from=$tn->columns item=co name=coname}>
<{$tn->tableName}>_columns[<{$smarty.foreach.coname.index}>]=new Array('<{$co->Field}>','<{$co->Field}>');


<{/foreach}>
tables['<{$tn->tableName}>']=<{$tn->tableName}>_columns;

<{/foreach}>
      
        

function test(){

	return false;

}
function   CheckForm()
{ 
	var aliasNames=new Array();


	
	//<{foreach from=$tempTables item=ttt name=foo}>
	
	aliasNames[<{$smarty.foreach.foo.index}>]= document.forms[0].aliasName<{$smarty.foreach.foo.index}>.value;
	//alert(aliasNames[<{$smarty.foreach.foo.index}>]);

	
	// <{/foreach}>
	
	
	var to=<{$tableSize}>;
	for(var i1=0;i1<to;i1++){

		for(var i2=0;i2<to;i2++){

			if(i2==i1)
			{

				continue;
			}
			if(aliasNames[i1]==aliasNames[i2]){

				var mess=new String("find alias name ["+aliasNames[i1]+"] are duplicated!");

				
				alert(mess);

				return false;
			}
		}
	}

}
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)

}

function updateTable(targetName,newTable) { 
	
	var columnObj=document.getElementById(targetName);


	if(columnObj==null){

		alert('debug:error target name:'+targetName);

		return;
	}
	
	
	// clear all old value
	for(var i=0;i<columnObj.options.length;i++){

		columnObj.options[i]=null;
	}

	var newColumns=tables[newTable];
	if(newColumns==null){

		alert('debug:error set name:'+newTable);

		return;
	}
	
	for(var i=0;i<newColumns.length;i++){

		columnObj.options[i]=new Option(newColumns[i][0],newColumns[i][1]);

	}
	
  	
  

}


//-->
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Arrange Tables</title>
</head>
	
<body>
<div align="center">
<form action="arrangeColumnAttributes.php" method="post" name="form1" id="form1" onsubmit="test();">
<table width="800" border="0">
  <tr>
      <td width="80">
      </td>
      <td width="90%">
          <table border="1" width="100%">
              <tr>
                <td width="50%">   
                     Table Name
                </td>
                <td width="50%">Alias Name (will be used as head of attributes)</td>
               </tr>
          </table>
      </td>
    
  </tr>

<{foreach from=$tempTables item=ttt name=foo}>
 
  
  <{if $ttt->flag==0}> 
  <tr bgcolor="#CC99FF"> 
    <td>Table setting:
  </td>
  <td width="100%">
  <table width="100%" border="1" >
  <tr>
    <td width="50%"> 
   
     <label>
      <select name="tables<{$ttt->index}>" id="tables<{$ttt->index}>">
      <{foreach from=$tables item=tn name=ind}>
        <option value="<{$tn->tableName}>" <{if $ttt->index== $smarty.foreach.ind.index}>selected="selected"<{/if}>><{$tn->tableName}></option> 
        <{/foreach}>
      </select>
    </label></td>
    <td width="50%"><input name="aliasName<{$ttt->index}>" type="text" value="<{if $smarty.foreach.foo.total >1}>t<{$ttt->index}><{else}><{/if}>"/></td>
  </tr>
  </table>
  <{else}>
  <tr bgcolor="#99FF00">
  <td>Join setting: 
  </td>
  <td width="100%">
  <table width="100%" border="1" >
  <tr>
  <td>
  <input name="join<{$ttt->index}>_method" type="text" value="join" size="8" /></td>
  <td width="50%">
  <select name="join<{$ttt->index}>_table1" onchange="javascript:updateTable('join<{$ttt->index}>_column1',this.value);">
          <{foreach from=$tables item=tn name=ind}>
        <option value="<{$tn->tableName}>" <{if $ttt->tables[0]== $tn->tableName}>selected<{/if}>="selected"><{$tn->tableName}></option> 
        <{/foreach}>
  </select>
  <select name="join<{$ttt->index}>_column1" id="join<{$ttt->index}>_column1">
  </select>
  <script language="javascript">updateTable('join<{$ttt->index}>_column1','<{$ttt->tables[0]}>')</script>
  </td>
  <td width="30">
  <select name="join<{$ttt->index}>_operation">
	<option  value="=" selected="selected">=</option>
	<option value="&gt;">&gt;</option>
	<option value="&lt;">&lt;</option>
	<option value="&gt;=">&gt;=</option>
	<option value="&lt;=">&lt;=</option>
  </select>
  </td>
  <td width="50%">
  <select name="join<{$ttt->index}>_table2" onchange="javascript:updateTable('join<{$ttt->index}>_column1',this.value);">
          <{foreach from=$tables item=tn name=ind}>
        <option value="<{$tn->tableName}>" <{if $ttt->tables[1]== $tn->tableName}>selected<{/if}>="selected"><{$tn->tableName}></option> 
        <{/foreach}>
  </select>
  <select name="join<{$ttt->index}>_column2" id="join<{$ttt->index}>_column2">
  </select>
  <script   language="javascript">updateTable('join<{$ttt->index}>_column2','<{$ttt->tables[1]}>')</script>
  </td>

  </tr>
  </table>
  <{/if}>
  </td>
  </tr>
  <{/foreach}>
</table>


    
      <label>Middle Words between table and attribute
        <input name="middleWord" type="text" id="middleWord" value="_" />
      </label>
    
    
      <label>
<input name="button" type="submit" id="button"  value="Submit" onclick= "javascript:return CheckForm();" />
      </label>
    
    
      <input name="totalTableSize" type="hidden" value="<{$tableSize}>" />
  
</form>
</div>
<br />
<br /></body>
</html>
